﻿#region 信息

// Copyright©2014 Rex Rao all rights reserved.
// File name : SoMcCoffee\Sohobloo.SoPermission.Web\GroupController.cs
// Author : Sohobloo
// Create time : 2014/02/09 20:00
// Update time : 2014/02/16 15:28

#endregion

#region 引用

using System;
using System.ComponentModel.Composition;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Sohobloo.SoCommon;
using Sohobloo.SoCommon.CommonExtension;
using Sohobloo.SoMcCoffee.Mvc;
using Sohobloo.SoPermission.Service;
using Sohobloo.SoPermission.Web.Models;

#endregion

namespace Sohobloo.SoPermission.Web.Controllers
{
    [Export]
    public class GroupController : MvcControlerBase
    {
        [Import]
        public IGroupService GroupService { get; set; }

        public ActionResult Index()
        {
            var groupViewModel = new GroupViewModel
            {
                AddGroupModel = new GroupModel(),
                EditGroupModel = new GroupModel()
            };
            return View(groupViewModel);
        }

        [HttpPost]
        public ActionResult List()
        {
            var groupList = GroupService.GetList();
            var groupModelList = groupList.Select(g => g.ToModel());

            return PartialView("_List", groupModelList);
        }

        public ActionResult OpenAdd()
        {
            return PartialView("_Add", new GroupModel());
        }

        [HttpPost]
        public ActionResult OpenEdit(Guid groupId)
        {
            var group = GroupService.Get(groupId);
            if(group.IsNull())
            {
                var jsExist = string.Format("AlertError('{0}');",
                    HttpUtility.JavaScriptStringEncode(HttpUtility.HtmlEncode("Group dose not exist.")));
                return JavaScript(jsExist);
            }
            var groupModel = group.ToModel();

            return PartialView("_Edit", groupModel);
        }

        [HttpPost]
        public ActionResult Add(GroupModel groupModel)
        {
            var result = TryExecute(() =>
            {
                var group = groupModel.ToEntity();
                return GroupService.Add(group);
            });

            switch(result.ResultType)
            {
                case OperationResultType.Success:
                    var jsSuccess = "GroupAdded();";
                    return JavaScript(jsSuccess);
                case OperationResultType.Exist:
                    var jsExist = string.Format("AlertError('{0}');",
                        HttpUtility.JavaScriptStringEncode(HttpUtility.HtmlEncode("Group name exists.")));
                    return JavaScript(jsExist);
            }
            var jsError = string.Format("AlertError('{0}');",
                HttpUtility.JavaScriptStringEncode(HttpUtility.HtmlEncode("Operation failed.")));
            return JavaScript(jsError);
        }

        [HttpPost]
        public ActionResult Edit(GroupModel groupModel)
        {
            var result = TryExecute(() =>
            {
                var group = GroupService.Get(groupModel.Id);
                groupModel.ToEntity(group);
                return GroupService.Update(group);
            });

            switch (result.ResultType)
            {
                case OperationResultType.Success:
                    var jsSuccess = "GroupUpdated();";
                    return JavaScript(jsSuccess);
                case OperationResultType.Exist:
                    var jsExist = string.Format("AlertError('{0}');",
                        HttpUtility.JavaScriptStringEncode(HttpUtility.HtmlEncode("Group name exists.")));
                    return JavaScript(jsExist);
            }
            var jsError = string.Format("AlertError('{0}');",
                HttpUtility.JavaScriptStringEncode(HttpUtility.HtmlEncode("Operation failed.")));
            return JavaScript(jsError);
        }

        [HttpPost]
        public ActionResult Delete(Guid groupId)
        {
            var result = TryExecute(() =>
            {
                return GroupService.Delete(groupId);
            });

            switch (result.ResultType)
            {
                case OperationResultType.Success:
                    var jsSuccess = "GroupDeleted();";
                    return JavaScript(jsSuccess);
                case OperationResultType.NoChange:
                    var jsExist = string.Format("AlertError('{0}');",
                        HttpUtility.JavaScriptStringEncode(HttpUtility.HtmlEncode("Group does not exist.")));
                    return JavaScript(jsExist);
            }
            var jsError = string.Format("AlertError('{0}');",
                HttpUtility.JavaScriptStringEncode(HttpUtility.HtmlEncode("Operation failed.")));
            return JavaScript(jsError);
        }
    }
}